home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / basictut.arc / A3.TXT < prev    next >
Text File  |  1989-01-07  |  7KB  |  156 lines

  1. -----
  2. DATA and READ Statements
  3.  
  4.         In the last lesson you learned about the INPUT statement.  Another way
  5.   to tell the computer something is with |DATA~ and |READ~ statements.
  6.  
  7.         The DATA statement is |not~ executed.  Data is taken from it when the
  8. computer does a READ statement.  After the word DATA, you put numbers or groups
  9. of letters in quotes separating them with commas.  This creates a DATA line.
  10. The |first~ time the computer comes across a READ statement it will read the
  11. |first~ value in the DATA line; the |second~ time, the |second~ value, and so on.
  12.  
  13.         Try typing in and |RUN~ning |either~ of the following two examples.
  14.  
  15.         |10 DATA 7,11                   10 DATA "FRED","BARNEY"~
  16.         |20 READ X                      20 READ X$~
  17.         |30 PRINT X                     30 PRINT X$~
  18.         |40 READ X                      40 READ X$~
  19.         |50 PRINT X                     50 PRINT X$~
  20. -----
  21. DATA and READ Statements (continued)
  22.  
  23.         When the computer runs out of data, an |error~ stops the program and an
  24. |error message~ is displayed.  Try typing in the following program.
  25.  
  26.         |10 DATA 10,"Twenty"~
  27.         |20 READ X~
  28.         |30 PRINT 1,X~
  29.         |40 READ X$~
  30.         |50 PRINT 2,X$~
  31.         |60 READ X~
  32.         |70 PRINT 3,X~
  33.         
  34.         Because there is no third value to read, the computer runs out of data
  35. and stops, giving an |error~ in line number |60~.  |RUN~ it and see what happens.
  36. -----
  37. GOTO Statement
  38.  
  39.         The |GOTO~ statement is the simplest to understand, yet one of the most
  40. important statements in BASIC.  It tells the computer to go to a different line
  41. number instead of doing the next one in order.
  42.  
  43.         In the following sample program, the computer will |loop~ (re-do over
  44. and over) lines |20~, |30~ and |40~ until it runs out of data.
  45. -----
  46. IF THEN Statement
  47.  
  48.         The |IF THEN~ statement gives the computer a |choice~ of what to do.  |IF~ a
  49. condition is |true~ the computer does what follows the word |THEN~, |IF~ it is |not~
  50. |true~, the computer goes to the |next line~.  Usually a GOTO statement will
  51. follow the THEN.
  52.  
  53.         |Relational operators~ compare two values in an IF THEN statement. The
  54. most common relational operators follow.
  55.  
  56.         |=~  equal to           |<~ less than        |<=~ less than or equal to
  57.         |<>~ not equal to       |>~ greater than     |>=~ greater than or equal to
  58. -----
  59. IF THEN Statement (continued)
  60.  
  61.         By placing a unique number at the end of a DATA line, and following the
  62. READ statement with an IF THEN statement to test for that number, you can avoid
  63. the |Out of DATA~ error.  This program finds the average of the numbers in a
  64. DATA statement, and uses |99~ as an |end of data~ signal.
  65. -----
  66. IF THEN Statement (continued)
  67.  
  68.         Here are a couple sample programs.  One will count the numeric
  69. constants in a DATA statement.  The other will pick the numbers greater than 50
  70. out of a DATA statement, and print them.  In both cases if the number is |999~
  71. the computer will stop.  Try typing in and |RUN~ning them.
  72.  
  73.         |10 DATA 10,69,2,45,100,74,12,999       10 DATA 10,69,2,45,100,74,12,999~
  74.         |20 READ X                              20 READ X~
  75.         |30 IF X=999 THEN GOTO 60               20 IF X=999 THEN STOP~
  76.         |40 LET K=K+1                           40 IF X<=50 THEN GOTO 20~
  77.         |50 GOTO 20                             50 PRINT X~
  78.         |60 PRINT K                             60 GOTO 20~
  79. -----
  80. Logical Operators
  81.  
  82.         |Logical operators~ can make the IF THEN statement more versatile.  The
  83. logical operators in IBM BASIC are:
  84.  
  85.                          |AND~, |OR~, |NOT~, |XOR~, |IMP~, |EQV~
  86.         
  87.         The first two are pretty straight forward, and are the most widely
  88. used.  Consult your IBM BASIC manual for truth tables and explanations of the
  89. other operators.  The |AND~ and |OR~ operators are used when you want to test two
  90. conditions with an |IF THEN~ statement.  Here is an example of their use in a
  91. program.  Try typing it in and |RUN~ning it, then change the values in the DATA
  92. statement and |RUN~ it again.
  93.  
  94.                 |10 DATA 5,23,17,34,29,45,999~
  95.                 |20 READ X~
  96.                 |30 IF X=999 THEN GOTO 70~
  97.                 |40 IF X<20 OR X>30 THEN LET A=A+1~
  98.                 |50 IF X>=20 AND X<=30 THEN LET B=B+1~
  99.                 |60 GOTO 20~
  100.                 |70 PRINT A;"numbers either less than 20 or more than 30 and";~
  101.                 |B;"between 20 and 30."~
  102. -----
  103. ELSE Statement
  104.  
  105.         Another |option~ of the IF THEN statement is the |ELSE~ statement.
  106. Without the ELSE, the program would |drop~ through the IF THEN and go to the
  107. next line when the condition of the IF THEN is false.  If the word ELSE follows
  108. the IF THEN, it will be executed rather than dropping through to the next line.
  109.  
  110.         Here is the last program modified to show the ELSE statement.  Type it
  111. in and |RUN~ it.
  112.  
  113.                 |10 DATA 5,23,17,34,29,45,999~
  114.                 |20 READ X~
  115.                 |30 IF X=999 THEN GOTO 60~
  116.                 |40 IF X<20 OR X>30 THEN LET A=A+1 ELSE LET B=B+1~
  117.                 |50 GOTO 20~
  118.                 |60 PRINT A;"numbers either less than 20 or more than 30 and";~
  119.                 |B;"between 20 and 30."~
  120. -----
  121. Multiple Statement Lines
  122.  
  123.         Another useful feature of BASIC on the IBM is the use of |multiple~
  124. |statement~ lines.  You can put two or more statements on the same line number by
  125. separating them with a colon (|:~).  This is very helpful with IF THEN
  126. statements.
  127.  
  128.         Here is a sample program that illustrates the use of multiple statement
  129. lines.  Type it in and |RUN~ it, then change the values in the DATA statement and
  130. |RUN~ it again.
  131.  
  132.         |10 DATA 5,23,17,34,29,45,999~
  133.         |20 READ X~
  134.         |30 IF X=999 THEN PRINT A;"lower than 20,";B;"between 20 and 30, and";C;~
  135.         |"above 30.":END~
  136.         |40 IF X<20 THEN PRINT X;"is less than 20.":LET A=A+1:GOTO 20~
  137.         |50 IF X<30 THEN PRINT X;"is between 20 and 30.":LET B=B+1:GOTO 20~
  138.         |60 PRINT X;"is greater than 30.":LET C=C+1:GOTO 20~
  139. -----
  140. End of Lesson Three
  141.  
  142.         You have reached the end of lesson three.  This lesson contains some of
  143. the most important concepts of programming.  Loops and conditionals will most
  144. likely make up the major portion of any program you write.  Be sure you have a
  145. thorough understanding of this lesson before going on to lesson four.
  146.  
  147.     If you are using the BASIC Prof,        |The PC-Prof.~
  148. let me know who you are!  Send your name        |P.O. Box 26~
  149. and address to:                        |Salina, Kansas~
  150.                             |67402-0026~
  151.  
  152.     If you like the Prof, include a contribution ($30 - $50 suggested) to
  153. help support development of additional volumes.  Please copy and share the
  154. Prof. with other IBM P.C. users.
  155. -----
  156.